package com.zx.ishopping.controller.User;


import com.zx.ishopping.DTO.UserChangePasswordDTO;
import com.zx.ishopping.DTO.UserInfoDTO;

import com.zx.ishopping.DTO.UserRegisterDTO;
import com.zx.ishopping.results.Result;
import com.zx.ishopping.services.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.swing.plaf.PanelUI;

@Slf4j
@RestController("UserUserController")
@RequestMapping("/user/user")
public class userController {

    @Autowired
    private UserService userService;
 
    
    /**
     * 获取验证码
     * @return 
     */
    
    @PostMapping("/getCaptcha")
    public Result getCaptcha(@RequestParam String email ){
        userService.sendAndStoreCaptcha(email);
        return Result.success();
    }


    /**
     * 用户注册服务
     * @param userRegisterDTO
     * @return
     */
    @PostMapping("/userRegister")
    public Result userRegister(@RequestBody UserRegisterDTO userRegisterDTO){
        userService.UserRegister(userRegisterDTO);
        return Result.success();
    }


    /**
     * 验证码校验
     * 同时判断该邮箱是否已注册
     */

    @PostMapping("/chackCaptcha")
    public Result<Boolean> chargeCaptcha(@RequestParam String inputCode,@RequestParam String email){
                Boolean res = userService.validateCaptcha( inputCode,email);
                return Result.success(res);
    }


    /**
     * 验证码正确
     * 通过邮箱来修改新密码
     */ 
    @PutMapping("/forgetPassword")
    public  Result forgetPassword(@RequestBody UserChangePasswordDTO userChangePasswordDTO){
        log.info("{}",userChangePasswordDTO);
        userService.forgetPassword(userChangePasswordDTO);
        return Result.success();
    }


    /**
     * 用户修改用户名和邮箱
     * @return
     */
    @PutMapping("/update")
    public Result changeUserInfo(@RequestBody UserInfoDTO userInfoDTO){
        userService.updateUserInfo(userInfoDTO);
        return Result.success();
    }


    /**
     * 用户主动修改密码
     * @param userChangePasswordDTO
     * @return
     */
    @PutMapping("/changePassword")
    public Result changeUserPassword(@RequestBody UserChangePasswordDTO userChangePasswordDTO){
        userService.changePassword(userChangePasswordDTO);
        return Result.success();
    }
        
    }
    

